package com.zdkj.subway.check.service.impl.subwayRat.conductorDifferences.SwAuConductorShort;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.subway.check.entity.SwStMarketingTicket;
import com.zdkj.subway.check.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SwAuConductorShortServiceImpl {

    @Autowired
    private SwAuConductorShortMapper swAuConductorShortMapper;
    @Autowired
    private SwStMarketingTicketMapper swStMarketingTicketMapper;
    @Autowired
    private SwStMarketingTicketGroupSellMapper swStMarketingTicketGroupSellMapper;
    @Autowired
    private SwStMarketingTicketOffineSellMapper swStMarketingTicketOffineSellMapper;
    @Autowired
    private SwStMarketingTicketOffineTakeTicketMapper swStMarketingTicketOffineTakeTicketMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 获取售票员补款
     * @param param
     * @return
     */
    public List<Map<String,Object>> getSwAuConductorShort(Map<String,String> param) {
        Map<String,Object> p1=new HashMap<>();
        String lineCode = param.get("lineCode");
        String stationCode = param.get("stationCode");
        if(StringUtils.isNotEmpty(lineCode) || StringUtils.isNotEmpty(stationCode)){
            List<Map<String, Object>> maps = swAuConductorShortMapper.getSwAuConductorShort(param);
            return maps;
        }
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
        List<Map<String,Object>> fareDifferencesInAudits=new ArrayList<>();
        for(int i=0;i<managementLineStations.size();i++){
            JSONObject jsonObject = managementLineStations.getJSONObject(i);
            String line = jsonObject.getString("line");
            String station = jsonObject.getString("station");
            param.put("lineCode",line);
            param.put("stationCode",station);
            List<Map<String,Object>> reportErrorAlterAppayOne = swAuConductorShortMapper.getSwAuConductorShort(param);
            fareDifferencesInAudits.addAll(reportErrorAlterAppayOne);
        }
        return fareDifferencesInAudits;
    }

    /**
     * 营销车票录入主表
     * @param page
     * @param param
     * @return
     */
    public IPage<SwStMarketingTicket> getSwStMarketingTicket(Page page, Map<String,Object> param) {
        /*Map<String,Object> p1=new HashMap<>();
        String lineCode = param.get("lineCode");
        String stationCode = param.get("stationCode");
        if(StringUtils.isNotEmpty(lineCode) || StringUtils.isNotEmpty(stationCode)){
            List<Map<String, Object>> maps = swStMarketingTicketMapper.getSwStMarketingTicket(param);
            return maps;
        }
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
        List<Map<String,Object>> fareDifferencesInAudits=new ArrayList<>();
        for(int i=0;i<managementLineStations.size();i++){
            JSONObject jsonObject = managementLineStations.getJSONObject(i);
            String line = jsonObject.getString("line");
            String station = jsonObject.getString("station");
            param.put("lineCode",line);
            param.put("stationCode",station);
            List<Map<String,Object>> swStMarketingTicket = swStMarketingTicketMapper.getSwStMarketingTicket(param);
            fareDifferencesInAudits.addAll(swStMarketingTicket);
        }*/
        IPage<SwStMarketingTicket> swStMarketingTicket = swStMarketingTicketMapper.getSwStMarketingTicket(page,param);
        return swStMarketingTicket;
    }

    /**
     * 线下正常售卖售票员
     * @param param
     * @return
     */
    public List<Map<String,Object>> getSwStMarketingTicketOffineSell(Map<String,String> param) {
        List<Map<String, Object>> swStMarketingTicketOffineSell = swStMarketingTicketOffineSellMapper.getSwStMarketingTicketOffineSell(param);
        return swStMarketingTicketOffineSell;
    }

    /**
     * 营销车票团购售卖
     * @param param
     * @return
     */
    public List<Map<String,Object>> getSwStMarketingTicketGroupSell(Map<String,String> param) {
        List<Map<String, Object>> swStMarketingTicketGroupSell = swStMarketingTicketGroupSellMapper.getSwStMarketingTicketGroupSell(param);
        return swStMarketingTicketGroupSell;
    }

    /**
     * 营收车票线下取票明细表
     * @param param
     * @return
     */
    public List<Map<String,Object>> getSwStMarketingTicketOffineTakeTicket(Map<String,String> param) {
        List<Map<String, Object>> swStMarketingTicketGroupSell = swStMarketingTicketOffineTakeTicketMapper.getSwStMarketingTicketOffineTakeTicket(param);
        return swStMarketingTicketGroupSell;
    }
}
